/*
 * 工具类-生成类文件头部共通信息
 *
 * VERSION  DATE        BY              REASON
 * -------- ----------- --------------- ------------------------------------------
 * 1.00     2014.07.24  wuxiaogang      程序・发布
 * -------- ----------- --------------- ------------------------------------------
 * Copyright 2014 easycode System. - All Rights Reserved.
 *
 */
package cn.com.softvan.easycode.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import cn.com.softvan.common.StrUtils;
import cn.com.softvan.common.Validator;
import cn.com.softvan.easycode.bean.ConfigBean;
import cn.com.softvan.easycode.bean.TableBean;
import cn.com.softvan.easycode.bean.TableExtBean;
/**
 * 工具类-生成类文件头部共通信息
 * @author wuxiaogang 
 *
 */
public class HeaderUtils {
	/**
	 * 生成 类头部信息
	 * @param config  配置文件-all--
	 * @param memo 类的=作用备注
	 * @param table 配置文件-表信息--
	 * @param type 1 bean 2 entity 3 daointer 4 mybatis xml 5 service interface 6 service impl 7 action 
	 * @return
	 */
	public static StringBuffer getHeader(ConfigBean config,
			String memo,
			TableBean table,
			int type){
		//存放内容
		StringBuffer sb=new StringBuffer("");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy.MM.dd");
		try {
			if(null==config){
				return sb;
			}
			//TODO ---1-----版本注释
			sb.append("/*	");
			sb.append("\n * "+table.getTableNote()+"  "+memo+"	");
			sb.append("\n *		");
			sb.append("\n * VERSION      DATE          BY              REASON		");
			sb.append("\n * -------- ----------- --------------- ------------------------------------------	");
			sb.append("\n * 1.00     "+sdf.format(new Date())+"      "+config.getAuther()+"         程序.发布		");
			sb.append("\n * -------- ----------- --------------- ------------------------------------------	");
			sb.append("\n * "+config.getCopyright()+"		");
			sb.append("\n *	");
			sb.append("\n */");
			
			// * @param srcPackage 类的包路径
			String srcPackage="";
			// * @param cExtBean 配置文件-all-扩展信息-- 优先级2(优先级越小 优先级越高)
			TableExtBean cExtBean=null;
			// * @param tExtBean 配置文件-表-扩展信息-- 优先级1(优先级越小 优先级越高)
			TableExtBean tExtBean=null;
			
			switch (type) {
				case 1://1 bean 
					srcPackage=config.getBeanPackage();
					cExtBean=config.getBeanExt();
					tExtBean=table.getBeanExt();
					break;
				case 2://2 entity
					srcPackage=config.getEntityPackage();
					cExtBean=config.getEntityExt();
					tExtBean=table.getEntityExt();
					break;
				case 3://3 daointer
					srcPackage=config.getDaointerPackage();
					cExtBean=config.getDaointerExt();
					tExtBean=table.getDaointerExt();				
					break;
				case 4://4 mybatis xml
					srcPackage=config.getXmlPackage();
					cExtBean=config.getXmlExt();
					tExtBean=table.getXmlExt();
					break;
				case 5://5 service interface
					srcPackage=config.getServicePackage();
					cExtBean=config.getServiceInterExt();
					tExtBean=table.getServiceInterExt();
					break;
				case 6://6 service impl
					srcPackage=config.getServicePackage()+".impl";
					cExtBean=config.getServiceExt();
					tExtBean=table.getServiceExt();
					break;
				case 7://7 action 
					srcPackage=config.getActionPackage();
					cExtBean=config.getActionExt();
					tExtBean=table.getActionExt();
					break;
				default:
					break;
			}
			//TODO----1.5--包名
			if(Validator.notEmpty(srcPackage)){
				sb.append("\npackage "+srcPackage+";");
			}
			//TODO ---2----引入依赖包
			if(tExtBean!=null 
					&& tExtBean.getImportclass()!=null 
					&& tExtBean.getImportclass().size()>0){
				for(String strclass:tExtBean.getImportclass()){
					sb.append("\nimport "+strclass);
				}
			}else if(cExtBean!=null 
					&& cExtBean.getImportclass()!=null 
					&& cExtBean.getImportclass().size()>0){
				for(String strclass:cExtBean.getImportclass()){
					sb.append("\nimport "+strclass);
				}
			}
			switch (type) {
				case 3:{//3 daointer
					String entityClassName="";
					if(Validator.isEmpty(table.getClassName())){
						entityClassName=StrUtils.toUpperCase(StrUtils.getRidprefixTableName(config.getRidprefixlen(),table.getTableName()),"_");
					}else{
						entityClassName=table.getClassName();
					}
					sb.append("\nimport "+config.getEntityPackage()+"."+entityClassName+";");
					break;}
				case 5:{//5 service interface
					String beanClassName="";
					if(Validator.isEmpty(table.getClassName())){
						beanClassName=StrUtils.toUpperCase(StrUtils.getRidprefixTableName(config.getRidprefixlen(),table.getTableName()),"_")+"Bean";
					}else{
						beanClassName=table.getClassName()+"Bean";
					}
					sb.append("\nimport "+config.getBeanPackage()+"."+beanClassName+";");
					break;}
				case 6:{//6 service impl
					String beanClassName="";
					String entityClassName="";
					String daointerClassName="";
					String serviceinterClassName="";
					if(Validator.isEmpty(table.getClassName())){
						entityClassName=StrUtils.toUpperCase(StrUtils.getRidprefixTableName(config.getRidprefixlen(),table.getTableName()),"_");
						beanClassName=StrUtils.toUpperCase(StrUtils.getRidprefixTableName(config.getRidprefixlen(),table.getTableName()),"_")+"Bean";
						daointerClassName="I"+StrUtils.toUpperCase(StrUtils.getRidprefixTableName(config.getRidprefixlen(),table.getTableName()),"_")+"Dao";
						serviceinterClassName="I"+StrUtils.toUpperCase(StrUtils.getRidprefixTableName(config.getRidprefixlen(),table.getTableName()),"_")+"Manager";
					}else{
						entityClassName=table.getClassName();
						beanClassName=table.getClassName()+"Bean";
						daointerClassName="I"+table.getClassName()+"Dao";
						serviceinterClassName="I"+table.getClassName()+"Manager";
					}
					if(Validator.notEmpty(table.getServiceName())){
						serviceinterClassName="I"+table.getServiceName();
					}
					sb.append("\nimport "+config.getEntityPackage()+"."+entityClassName+";");
					sb.append("\nimport "+config.getBeanPackage()+"."+beanClassName+";");
					sb.append("\nimport "+config.getDaointerPackage()+"."+daointerClassName+";");
					sb.append("\nimport "+config.getServicePackage()+"."+serviceinterClassName+";");
					break;}
				case 7:{//7 action 
					String beanClassName="";
					String serviceinterClassName="";
					if(Validator.isEmpty(table.getClassName())){
						beanClassName=StrUtils.toUpperCase(StrUtils.getRidprefixTableName(config.getRidprefixlen(),table.getTableName()),"_")+"Bean";
						serviceinterClassName="I"+StrUtils.toUpperCase(StrUtils.getRidprefixTableName(config.getRidprefixlen(),table.getTableName()),"_")+"Manager";
					}else{
						beanClassName=table.getClassName()+"Bean";
						serviceinterClassName="I"+table.getClassName()+"Manager";
					}
					if(Validator.notEmpty(table.getServiceName())){
						serviceinterClassName="I"+table.getServiceName();
					}
					sb.append("\nimport "+config.getBeanPackage()+"."+beanClassName+";");
					sb.append("\nimport "+config.getServicePackage()+"."+serviceinterClassName+";");
					break;}
				default:
					break;
			}
			
			//TODO ---3----类注释
			sb.append("\n/**");
			sb.append("\n * <p>"+table.getTableNote()+"  "+memo+"。</p>	");
			if(type==5||type==6){
				sb.append("\n * <ol>[功能概要] ");
				sb.append("\n * <div>编辑(新增or修改)。</div> ");
				sb.append("\n * <div>详情检索。</div> ");
				sb.append("\n * <div>分页检索。</div> ");
				sb.append("\n * <div>列表检索。</div> ");
				sb.append("\n * <div>逻辑删除。</div> ");
				sb.append("\n * <div>物理删除。</div> ");
				sb.append("\n * <div>恢复逻辑删除。</div> ");
				sb.append("\n *</ol> ");
			}
			if(type==7){
				sb.append("\n * <ol>[功能概要] ");
				sb.append("\n * <div>初始化。</div> ");
				sb.append("\n * <div>信息列表(未删除)。</div> ");
				sb.append("\n * <div>编辑页面(页面)(新增or修改)。</div> ");
//				sb.append("\n * <div>信息保存(功能)(新增or修改)。</div> ");
//				sb.append("\n * <div>预览(页面)。</div> ");
//				sb.append("\n * <div>回收站(页面)。</div> ");
//				sb.append("\n * <div>逻辑删除(功能)。</div> ");
//				sb.append("\n * <div>物理删除(功能)。</div> ");
//				sb.append("\n * <div>恢复逻辑删除(功能)。</div> ");
				sb.append("\n *</ol> ");
			}
			sb.append("\n * @author "+config.getAuther());
			sb.append("\n */");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb;
	}
	/**
	 * 拼装 父类继承与接口实现
	 * @param config
	 * @param table
	 * @param type 1 bean 2 entity 3 daointer 4 mybatis xml 5 service interface 6 service impl 7 action 
	 * @return
	 */
	public static StringBuffer setExtendsAndimplements(ConfigBean config,TableBean table,int type){
		StringBuffer sb=new StringBuffer("");
		TableExtBean cExtBean =null;
		TableExtBean tExtBean =null;
		switch (type) {
			case 1://1 bean 
				cExtBean=config.getBeanExt();
				tExtBean=table.getBeanExt();
				break;
			case 2://2 entity
				cExtBean=config.getEntityExt();
				tExtBean=table.getEntityExt();
				break;
			case 3://3 daointer
				cExtBean=config.getDaointerExt();
				tExtBean=table.getDaointerExt();				
				break;
			case 4://4 mybatis xml
				cExtBean=config.getXmlExt();
				tExtBean=table.getXmlExt();
				break;
			case 5://5 service interface
				cExtBean=config.getServiceInterExt();
				tExtBean=table.getServiceInterExt();
				break;
			case 6://6 service impl
				cExtBean=config.getServiceExt();
				tExtBean=table.getServiceExt();
				break;
			case 7://7 action 
				cExtBean=config.getActionExt();
				tExtBean=table.getActionExt();
				break;
			default:
				break;
		}
		//TODO--3.1--继承----
		if(tExtBean!=null 
				&& Validator.notEmpty(tExtBean.getExtendsClass())){
			sb.append(" extends "+tExtBean.getExtendsClass());
		}else if(cExtBean!=null 
				&& Validator.notEmpty(cExtBean.getExtendsClass())){
			sb.append(" extends "+cExtBean.getExtendsClass());
		}
		String serviceinterClassName="";
		if(type==6){//业务逻辑处理实现类
			if(Validator.isEmpty(table.getClassName())){
				serviceinterClassName="I"+StrUtils.toUpperCase(StrUtils.getRidprefixTableName(config.getRidprefixlen(),table.getTableName()),"_")+"Manager";
			}else{
				serviceinterClassName="I"+table.getClassName()+"Manager";
			}
			if(Validator.notEmpty(table.getServiceName())){
				serviceinterClassName="I"+table.getServiceName();
			}
		}
		//TODO--3.2--接口实现----
		if(tExtBean!=null 
				&& Validator.notEmpty(tExtBean.getImplClass())){
			if(Validator.notEmpty(serviceinterClassName)){
				sb.append(" implements "+serviceinterClassName+","+tExtBean.getImplClass());
			}else{
				sb.append(" implements "+tExtBean.getImplClass());
			}
		}else if(cExtBean!=null 
				&& Validator.notEmpty(cExtBean.getImplClass())){
			if(Validator.notEmpty(serviceinterClassName)){
				sb.append(" implements "+serviceinterClassName+","+cExtBean.getImplClass());
			}else{
				sb.append(" implements "+cExtBean.getImplClass());
			}
		}else{
			if(Validator.notEmpty(serviceinterClassName)){
				sb.append(" implements "+serviceinterClassName);
			}
		}
		return sb;
	}
	/**
	 * 获取18位数字的随机数
	 * @return
	 */
	public static String getbigInt(){
		String s = "";
		  Random random = new Random();
		  s+=random.nextInt(9)+1; 
		  for (int i = 0; i < 18-1; i++) {
		            s+=random.nextInt(10); 
		  }
//		  BigInteger bigInteger=new BigInteger(s);
//		  System.out.println(bigInteger);
		  return s;
	}
	/**
	 * 属性设定
	 * @param field 属性名称
	 * @param annotation 属性注释
	 * @param dafault 属性默认值
 	 * @param type 属性java类型
	 * @return
	 */
	public static StringBuffer setInfo(ConfigBean config,String field,String annotation,String dafault,String type){
		StringBuffer sb=new StringBuffer("");
		
		if(config.getBaseColumns()!=null && config.getBaseColumns().contains(StrUtils.toLowerCase(field))){
			return sb;
		}
		
		if("1".equals(config.getIsformat())){
			field=(StrUtils.toLowerCaseFirstOne(StrUtils.toUpperCase(field,"_")));
		}
		//--2.1--设定----
		//注释
		sb.append("\n\t/**");
		sb.append("\n\t * "+annotation);
		if(Validator.notEmpty(dafault)){
			sb.append("  默认值("+dafault+")");
		}
		sb.append(" 设定 ");
		sb.append("\n\t * @param "+field+""+annotation);
		sb.append("\n\t */");
		//字段
		sb.append("\n\tpublic void set"+StrUtils.toUpperCaseFirstOne(field)+"("+type+" "+field+"){");
		sb.append("\n\t\tthis."+field+"="+field+";");
		sb.append("\n\t}");
		return sb;
	}
	/**
	 * 属性取得
	 * @param field 属性名称
	 * @param annotation 属性注释
	 * @param dafault 属性默认值
 	 * @param type 属性java类型
	 * @return
	 */
	public static StringBuffer getInfo(ConfigBean config,String field,String annotation,String dafault,String type){
		StringBuffer sb=new StringBuffer("");
		if(config.getBaseColumns()!=null && config.getBaseColumns().contains(StrUtils.toLowerCase(field))){
			return sb;
		}
		if("1".equals(config.getIsformat())){
			field=(StrUtils.toLowerCaseFirstOne(StrUtils.toUpperCase(field,"_")));
		}
		//注释
		sb.append("\n\t/**");
		sb.append("\n\t * "+annotation);
		if(Validator.notEmpty(dafault)){
			sb.append("  默认值("+dafault+")");
		}
		sb.append(" 取得 ");
		sb.append("\n\t * @return "+annotation);
		sb.append("\n\t */");
		//字段
		sb.append("\n\tpublic "+type+" get"+StrUtils.toUpperCaseFirstOne(field)+"(){");
		sb.append("\n\t\treturn "+field+";");
		sb.append("\n\t}");
		return sb;
	}
	/**
	 * 获取字段
	 * @param isFormat 1格式化 2不格式化
	 * @param column 字段名称
	 * @return
	 */
	public static String getStr(String isFormat,String column){
		if("1".equals(isFormat)){
			return StrUtils.toLowerCaseFirstOne(StrUtils.toUpperCase(column,"_"));
		}else{
			return column;
		}
	}
}
